#include"equipWidget.h"

EquipWidget::EquipWidget(QWidget* p):QWidget(p),ui(new Ui::EquipWidget),filePath("")
{
    ui->setupUi(this);
    setInputMask();
}

EquipWidget::~EquipWidget()
{
    delete ui;
}

void EquipWidget::setInputMask()
{
    QIntValidator* charValidator = new QIntValidator(-128,127,this);
    QIntValidator* intValidator = new QIntValidator(this);
    ui->lineEdit_equip_starlevel->setValidator(new QIntValidator(0,10,this));
    ui->lineEdit_equip_attackMin->setValidator(intValidator);
    ui->lineEdit_equip_attackMax->setValidator(intValidator);
    ui->lineEdit_equip_defenseMin->setValidator(intValidator);
    ui->lineEdit_equip_defenseMax->setValidator(intValidator);
    ui->lineEdit_equip_energyRecoverMax->setValidator(charValidator);
    ui->lineEdit_equip_energyRecoverMin->setValidator(charValidator);
    ui->lineEdit_equip_ammoRecoverMax->setValidator(charValidator);
    ui->lineEdit_equip_ammoRecoverMin->setValidator(charValidator);
    ui->lineEdit_equip_expAddonMax->setValidator(charValidator);
    ui->lineEdit_equip_expAddonMin->setValidator(charValidator);
    ui->lineEdit_equip_cashAddonMax->setValidator(charValidator);
    ui->lineEdit_equip_cashAddonMin->setValidator(charValidator);
}

Equipment EquipWidget::getEquipmentFromUI()
{
    Equipment e;
    e.id = (qint32) ui->lineEdit_equip_id->text().toInt();
    e.type = (qint8) ui->comboBox_equip_type->currentIndex();
    e.name = ui->lineEdit_equip_name->text();
    e.description = ui->plainTextEdit_equip_description->toPlainText();
    e.star_level = (qint8)ui->lineEdit_equip_starlevel->text().toInt();
    e.attack_min = (qint32)ui->lineEdit_equip_attackMin->text().toInt();
    e.attack_max = (qint32)ui->lineEdit_equip_attackMax->text().toInt();
    e.defense_min = (qint32)ui->lineEdit_equip_defenseMin->text().toInt();
    e.defense_max = (qint32)ui->lineEdit_equip_defenseMax->text().toInt();
    e.energy_recover_min = (qint8)ui->lineEdit_equip_energyRecoverMin->text().toInt();
    e.energy_recover_max = (qint8)ui->lineEdit_equip_energyRecoverMax->text().toInt();
    e.ammo_recover_min = (qint8)ui->lineEdit_equip_ammoRecoverMin->text().toInt();
    e.ammo_recover_max = (qint8)ui->lineEdit_equip_ammoRecoverMax->text().toInt();
    e.exp_addon_min = (qint8)ui->lineEdit_equip_expAddonMin->text().toInt();
    e.exp_addon_max = (qint8)ui->lineEdit_equip_expAddonMax->text().toInt();
    e.money_addon_min = (qint8)ui->lineEdit_equip_cashAddonMin->text().toInt();
    e.money_addon_max = (qint8)ui->lineEdit_equip_cashAddonMax->text().toInt();
    return e;
}

void EquipWidget::displayEquipmentToUI(Equipment& e)
{
    clearUI();

    ui->lineEdit_equip_id->setText(QString::number(e.id));
    ui->comboBox_equip_type->setCurrentIndex(e.type);
    ui->lineEdit_equip_name->setText(e.name);
    ui->plainTextEdit_equip_description->setPlainText(e.description);
    ui->lineEdit_equip_starlevel->setText(QString::number(e.star_level));
    ui->lineEdit_equip_attackMin->setText(QString::number(e.attack_min));
    ui->lineEdit_equip_attackMax->setText(QString::number(e.attack_max));
    ui->lineEdit_equip_defenseMin->setText(QString::number(e.defense_min));
    ui->lineEdit_equip_defenseMax->setText(QString::number(e.defense_max));
    ui->lineEdit_equip_energyRecoverMin->setText(QString::number(e.energy_recover_min));
    ui->lineEdit_equip_energyRecoverMax->setText(QString::number(e.energy_recover_max));
    ui->lineEdit_equip_ammoRecoverMin->setText(QString::number(e.ammo_recover_min));
    ui->lineEdit_equip_ammoRecoverMax->setText(QString::number(e.ammo_recover_max));
    ui->lineEdit_equip_expAddonMin->setText(QString::number(e.exp_addon_min));
    ui->lineEdit_equip_expAddonMax->setText(QString::number(e.exp_addon_max));
    ui->lineEdit_equip_cashAddonMin->setText(QString::number(e.money_addon_min));
    ui->lineEdit_equip_cashAddonMax->setText(QString::number(e.money_addon_max));
}

Equipment EquipWidget::readEquipmentFromFile()
{
    Equipment e;
    QFile f(filePath);
    if(f.exists())
    {
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>e;
//        cout<<e;
        f.close();
        displayEquipmentToUI(e);
    }else
        WARNING_FILE_NOT_EXIST;
    return e;
}

bool EquipWidget::saveEquipmentToFile()
{
    if(ui->lineEdit_equip_name->text().isEmpty()||filePath.isEmpty())
    {
        WARNING_NO_NAME;
        return false;
    }
    else
    {
        Equipment e = getEquipmentFromUI();
        QFile f(filePath);
        f.open(QIODevice::WriteOnly);
        QDataStream out(&f);
        out<<e;
        f.close();

        int idx1 = filePath.lastIndexOf("/");
        int idx2 = filePath.lastIndexOf(".");

        filePath.replace(idx1+1,idx2-idx1-1,ui->lineEdit_equip_name->text());

        f.rename(filePath);
        return true;
    }
}

void EquipWidget::clearUI()
{
    ui->lineEdit_equip_id->clear();
    ui->lineEdit_equip_name->clear();
    ui->plainTextEdit_equip_description->clear();
    ui->lineEdit_equip_starlevel->clear();
    ui->lineEdit_equip_attackMin->clear();
    ui->lineEdit_equip_attackMax->clear();
    ui->lineEdit_equip_defenseMin->clear();
    ui->lineEdit_equip_defenseMax->clear();
    ui->lineEdit_equip_energyRecoverMin->clear();
    ui->lineEdit_equip_energyRecoverMax->clear();
    ui->lineEdit_equip_ammoRecoverMin->clear();
    ui->lineEdit_equip_ammoRecoverMax->clear();
    ui->lineEdit_equip_expAddonMin->clear();
    ui->lineEdit_equip_expAddonMax->clear();
    ui->lineEdit_equip_cashAddonMin->clear();
    ui->lineEdit_equip_cashAddonMax->clear();
}

void EquipWidget::exportToBinary(QString workspacepath)
{
    //export equip
    QFile f2(workspacepath+"/"+EQUIP_EXPORT_NAME);
    f2.open(QIODevice::WriteOnly);
    QDataStream out2(&f2);

    out2<<(qint32)Equipment::magic;
    out2<<(qint32)Equipment::version;

    QDir dir_equipType(workspacepath+"/"+EQUIP_STR);
    QStringList equipTypeList = dir_equipType.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
    for(int i=0;i<equipTypeList.length();i++)
    {
        QDir dir_concreteEquip(dir_equipType.absolutePath()+"/"+equipTypeList.at(i));
        QString mfilter = QString("*")+QString(EQUIP_FILE_SUFFIX);
        QStringList equipList = dir_concreteEquip.entryList(QStringList(mfilter));
        for(int j=0;j<equipList.length();j++)
        {
            Equipment e;
            QFile f21(dir_concreteEquip.absolutePath()+"/"+equipList.at(j));
            f21.open(QIODevice::ReadOnly);
            QDataStream in(&f21);
            in>>e;
            out2<<e;
            f21.close();
        }
    }
    f2.close();
}

void EquipWidget::exportToXML(QString workspacepath)
{
    //*****write XML*******//
    QFile file(workspacepath+"/output/equip.xml");
    file.open(QIODevice::WriteOnly);
    QDomDocument doc;
    QDomText text;
    QDomProcessingInstruction instruction;
    instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"gb18030\"");
    doc.appendChild(instruction);
    QDomElement root=doc.createElement("root");
    doc.appendChild(root);

    QDir dir_type_root(workspacepath+"/"+EQUIP_STR);
    QStringList typeList = dir_type_root.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
    int cnt_type = typeList.length();
    for(int i=0;i<cnt_type;i++)
    {
        QString tname;
        if(typeList.at(i)==QString::fromLocal8Bit(WEAPON_STR))
        {
            tname = "weapon";
        }else if(typeList.at(i)==QString::fromLocal8Bit(ARMOR_STR))
        {
            tname = "armor";
        }else if(typeList.at(i)==QString::fromLocal8Bit(HORSE_STR))
        {
            tname = "horse";
        }else
        {
            continue;
        }
        QDomElement typeElement=doc.createElement(tname);
        root.appendChild(typeElement);

        QDir dir_item(dir_type_root.absolutePath()+"/"+typeList.at(i));
        QStringList itemList = dir_item.entryList(QStringList()<<(QString("*")+EQUIP_FILE_SUFFIX));
        for(int j=0;j<itemList.length();j++)
        {
            QDomElement equip=doc.createElement("equip");
            typeElement.appendChild(equip);

            Equipment e;
            QFile mf(dir_item.absolutePath()+"/"+itemList.at(j));
            mf.open(QIODevice::ReadOnly);
            QDataStream in(&mf);
            in>>e;
            mf.close();

            QDomElement equipElement;

            equipElement=doc.createElement("id");
            text=doc.createTextNode(QString::number(e.id));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("type");
            text=doc.createTextNode(QString::number(e.type));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("name");
            text=doc.createTextNode(e.name);
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("description");
            text=doc.createTextNode(e.description);
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("star_level");
            text=doc.createTextNode(QString::number(e.star_level));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("attack_min");
            text=doc.createTextNode(QString::number(e.attack_min));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("attack_max");
            text=doc.createTextNode(QString::number(e.attack_max));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("defense_min");
            text=doc.createTextNode(QString::number(e.defense_min));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("defense_max");
            text=doc.createTextNode(QString::number(e.defense_max));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("energy_recover_min");
            text=doc.createTextNode(QString::number(e.energy_recover_min));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("energy_recover_max");
            text=doc.createTextNode(QString::number(e.energy_recover_max));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("ammo_recover_min");
            text=doc.createTextNode(QString::number(e.ammo_recover_min));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("ammo_recover_max");
            text=doc.createTextNode(QString::number(e.ammo_recover_max));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("exp_addon_min");
            text=doc.createTextNode(QString::number(e.exp_addon_min));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("exp_addon_max");
            text=doc.createTextNode(QString::number(e.exp_addon_max));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("money_addon_min");
            text=doc.createTextNode(QString::number(e.money_addon_min));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);

            equipElement=doc.createElement("money_addon_max");
            text=doc.createTextNode(QString::number(e.money_addon_max));
            equipElement.appendChild(text);
            equip.appendChild(equipElement);
        }
    }

    QTextStream out(&file);
    doc.save(out,4);
    file.close();
}

bool EquipWidget::importFromXML(QString workspacepath,QString xmlFile)
{
    QFile f(xmlFile);
    f.open(QIODevice::ReadOnly|QIODevice::Text);

    QDomDocument docment;

    if(!docment.setContent(&f,false))
    {
        return false;
    }

    QDir dir_root(workspacepath+"/"+EQUIP_STR);
    QDomElement root = docment.documentElement();

    QDomNodeList typeChildren = root.childNodes();
    if(typeChildren.length()!=3)
        return false;
    for(int i=0;i<typeChildren.length();i++)
    {
        QString typeName;
        if(typeChildren.at(i).toElement().tagName()==QString("weapon"))
        {
            typeName = QString::fromLocal8Bit(WEAPON_STR);
        }else if(typeChildren.at(i).toElement().tagName()==QString("armor"))
        {
            typeName = QString::fromLocal8Bit(ARMOR_STR);
        }else if(typeChildren.at(i).toElement().tagName()==QString("horse"))
        {
            typeName = QString::fromLocal8Bit(HORSE_STR);
        }else
            continue;

        QDir typeDir(dir_root.absolutePath()+"/"+typeName);

        QDomNodeList elementList = typeChildren.at(i).toElement().childNodes();

        for(int j=0;j<elementList.length();j++)
        {
            QDomNodeList equipAtr = elementList.at(j).toElement().childNodes();

            Equipment e;
            e.id = (qint32)equipAtr.at(0).toElement().text().toInt();
            e.type = (qint8)equipAtr.at(1).toElement().text().toInt();
            e.name = equipAtr.at(2).toElement().text();
            e.description = equipAtr.at(3).toElement().text();
            e.star_level = (qint8)equipAtr.at(4).toElement().text().toInt();
            e.attack_min = (qint32)equipAtr.at(5).toElement().text().toInt();
            e.attack_max = (qint32)equipAtr.at(6).toElement().text().toInt();
            e.defense_min = (qint32)equipAtr.at(7).toElement().text().toInt();
            e.defense_max = (qint32)equipAtr.at(8).toElement().text().toInt();
            e.energy_recover_min = (qint8)equipAtr.at(9).toElement().text().toInt();
            e.energy_recover_max = (qint8)equipAtr.at(10).toElement().text().toInt();
            e.ammo_recover_min = (qint8)equipAtr.at(11).toElement().text().toInt();
            e.ammo_recover_max = (qint8)equipAtr.at(12).toElement().text().toInt();
            e.exp_addon_min = (qint8)equipAtr.at(13).toElement().text().toInt();
            e.exp_addon_max = (qint8)equipAtr.at(14).toElement().text().toInt();
            e.money_addon_min = (qint8)equipAtr.at(15).toElement().text().toInt();
            e.money_addon_max = (qint8)equipAtr.at(16).toElement().text().toInt();

            QString equipName = e.name+QString(EQUIP_FILE_SUFFIX);

//            qDebug()<<equipName;

            QFile mf(typeDir.absolutePath()+"/"+equipName);
            mf.open(QIODevice::WriteOnly);
            QDataStream out(&mf);
            out<<e;
            mf.close();
        }
    }
    return true;
}

